<template>
  <el-card>
    <el-form :inline="true" :model="data" class="demo-form-inline">
      <el-form-item>
        <el-input @change="inputEnter" v-model="data.orderNo" placeholder="请输入订单号" clearable />
      </el-form-item>
      <el-form-item>
        <el-select
          @change="changeSelect"
          v-model="data.orderStatus"
          placeholder="请输入订单状态"
          style="width: 200px"
        >
          <el-option
            v-for="(item, index) in arr"
            :key="index"
            :label="item.name"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button :icon="HomeFilled" color="#1baeae" @click="peiHuoAll"
          >配货完成</el-button
        >
        <el-button :icon="HomeFilled" color="#1baeae" @click="chuHuoAll"
          >出库</el-button
        >
        <el-button :icon="Delete" color="#f56c6c" @Click="closeAll"
          >关闭订单</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      @selection-change="changeSelectStatus"
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column type="selection" />
      <el-table-column prop="orderNo" label="订单号" />
      <el-table-column prop="totalPrice" label="订单总价" />
      <el-table-column prop="orderStatus" label="订单状态">
        <template #default="scope">
          <p>
            {{ arr.find((item) => item.value === scope.row.orderStatus)?.name }}
          </p>
        </template>
      </el-table-column>
      <el-table-column prop="payType" label="支付方式">
        <template #default="scope">
          {{
            scope.row.payType == 0
              ? "未支付"
              : scope.row.payType == 1
              ? "微信支付"
              : "支付宝支付"
          }}
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" />
      <el-table-column prop="address" label="操作" width="400">
        <template #default="scope">
          <el-button
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 2"
            @click="deliverGoods([scope.row.orderId])"
            >出库</el-button
          >
          <el-button style="color: #409eff" type="primary" text
          @click="orderDetail(scope.row.orderId)"
            >订单详情</el-button
          >
          <el-button
            @click="closeOrder([scope.row.orderId])"
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 0"
            >关闭订单</el-button
          >
          <el-button
            @click="closeOrder([scope.row.orderId])"
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 1"
            >关闭订单</el-button
          >
          <el-button
            @click="closeOrder([scope.row.orderId])"
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 2"
            >关闭订单</el-button
          >
          <el-button
            @click="closeOrder([scope.row.orderId])"
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 3"
            >关闭订单</el-button
          >
          <el-button
            text
            style="color: #409eff"
            v-if="scope.row.orderStatus == 1"
            @click="deliveryOrder([scope.row.orderId])"
            >配货完成</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        v-model:current-page="data.pageNumber"
        v-model:page-size="data.pageSize"
        :page-sizes="[10, 20, 30, 40]"
        :background="true"
        layout=" prev, pager, next"
        :total="totalCount"
        @size-change="getList"
        @current-change="getList"
      />
    </div>
  </el-card>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { useRoute, useRouter } from "vue-router";
import { api } from "../../../api/api";
import { HomeFilled, Delete } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import order_detail from "./order_detail.vue";
const route = useRoute();
const router = useRouter();
const tableData = ref([]);
const totalCount = ref(0);
const data = reactive({
  pageNumber: 1,
  pageSize: 10,
  orderNo: "",
  orderStatus: "",
});
async function getList() {
  const res: any = await api.getOrdersListApi(data);
  totalCount.value = res.data.totalCount;
  tableData.value = res.data.list;
}
onMounted(() => {
  getList();
});
const arr = ref([
  {
    name: "全部",
    value: -10,
  },
  {
    name: "待支付",
    value: 0,
  },
  {
    name: "已支付",
    value: 1,
  },
  {
    name: "配货完成",
    value: 2,
  },
  {
    name: "出库成功",
    value: 3,
  },
  {
    name: "交易成功",
    value: 4,
  },
  {
    name: "手动关闭",
    value: -1,
  },
  {
    name: "超时关闭",
    value: -2,
  },
  {
    name: "商家关闭",
    value: -3,
  },
]);

function changeSelect(e: any) {
  if (e != -10) data.orderStatus = e;
  else data.orderStatus = "";
  getList();
}
// 表格复选框状态
const multipleSelection = ref<number[]>([]);
function changeSelectStatus(e: any) {
  multipleSelection.value = [];
  e.forEach((element: any) => {
    multipleSelection.value.push(element.orderId);
  });
}
// 关闭订单
async function closeOrder(ids: number[]) {
  const res: any = await api.closeOrdersApi({ ids: ids });
  if (res.resultCode !== 200) ElMessage.error(res.message);
  else ElMessage.success("成功");
  getList();
}
// 多选关闭
function closeAll() {
  closeOrder(multipleSelection.value);
}
// 配货完成
async function deliveryOrder(ids: number[]) {
  const res: any = await api.checkDoneApi({ ids: ids });
  if (res.resultCode !== 200) ElMessage.error(res.message);
  else ElMessage.success("成功");
  getList();
}
// 多选配货完成
function peiHuoAll() {
  deliveryOrder(multipleSelection.value);
}
// 出货
async function deliverGoods(ids: number[]) {
  const res: any = await api.checkOutApi({ ids: ids });
  if (res.resultCode !== 200) ElMessage.error(res.message);
  else ElMessage.success("成功");
  getList();
}

// 多选出货完成
function chuHuoAll() {
  deliverGoods(multipleSelection.value);
}
// 输入框回车搜索
function inputEnter(e:any){
    data.orderNo=e
    getList()
}
// 跳转订单详情
function orderDetail(id: number){
  router.push({path:'/order_detail',query:{id:id}})
}
</script>

<style lang="scss" scoped>
.el-button {
  color: #fff;
}
.pagination {
  width: 100%;
  display: flex;
  margin: 20px 0;
  justify-content: center;
  align-items: center;
}
</style>
